Passed
Branch feature/first-draft (521840)
by Pieter Epeüs
02:27
created

app-error.js ➔ hasErrors   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
1
import Validator from '@hckrnews/validator';
2
import errorSchema from './schemas/error';
3
4
const validator = new Validator(errorSchema);
0 ignored issues
show
Unused Code introduced by
The constant validator seems to be never used. Consider removing it.
Loading history...
5
6
export default (error = Error) =>
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
7
    class AppError extends error {
8
        constructor({ value = null, type = null, message }) {
9
            super(message);
10
11
            if (Error.captureStackTrace) {
12
                Error.captureStackTrace(this, AppError);
13
            }
14
15
            this.setValues({ value, type });
16
        }
17
18
        get name() {
19
            return 'AppError';
20
        }
21
22
        get errorStatus() {
23
            return 500;
24
        }
25
26
        get status() {
27
            return this.hasErrors ? 500 : this.errorStatus;
28
        }
29
30
        setValues({ value, type }) {
31
            this.value = value;
32
            this.type = type;
33
            this.date = new Date();
34
35
            if (!this.validate()) {
36
                this.type = Error;
37
                this.message = 'Invalid error';
38
                this.value = { errors: validator.errors, values: this.values };
0 ignored issues
show
Bug introduced by
The variable validator seems to be never declared. If this is a global, consider adding a /** global: validator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
39
            }
40
        }
41
42
        get hasErrors() {
43
            return validator.errors.length > 0;
0 ignored issues
show
Bug introduced by
The variable validator seems to be never declared. If this is a global, consider adding a /** global: validator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
44
        }
45
46
        get values() {
47
            return {
48
                name: this.name,
49
                message: this.message,
50
                value: this.value,
51
                status: this.errorStatus,
52
                type: this.type,
53
                date: this.date,
54
            };
55
        }
56
57
        validate() {
58
            return validator.validate(this.values);
0 ignored issues
show
Bug introduced by
The variable validator seems to be never declared. If this is a global, consider adding a /** global: validator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
59
        }
60
    };
61